package cn.green.manage.service;

import cn.green.core.model.QCondition;
import cn.green.manage.cache.QCache;
import cn.green.manage.curd.QConditionCurd;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wang.yishu
 * @version 1.0
 * @className QConditionService
 * @description 查询条件service
 * @date 2025/1/10 9:55
 */
@Transactional(readOnly = true)
public class QConditionService {

    public QConditionService(QConditionCurd conditionDao, QCache qCache) {
        this.qConditionCurd = conditionDao;
        this.qCache = qCache;
    }

    private final QConditionCurd qConditionCurd;

    private final QCache qCache;
    
    public QCondition getCondition(String source, String scheme, String tableName) {
//        if (!qCache.conditionExistInCache(source, scheme, tableName)) {
//            QCondition conditionInfo = this.qConditionCurd.getConditionInfo(source, scheme,tableName);
//            if (conditionInfo == null) {
//                conditionInfo = new QCondition();
//                conditionInfo.setTableName(tableName);
//            }
//            conditionInfo.transformJson();
//            qCache.setConditionToCache(conditionInfo);
//        }
//        return qCache.getConditionFromCache(source, scheme,tableName);
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveCondition(QCondition condition) {
//        String sourceName = condition.getSourceName();
//        if (sourceName == null || sourceName.isEmpty()) {
//            throw new RuntimeException("表所在的数据源名称不能为空！");
//        }
//        String tableName = condition.getTableName();
//        if (tableName == null || tableName.isEmpty()) {
//            throw new RuntimeException("表名称不能为空！");
//        }
//        String schemeName = condition.getSchemeName();
//        if (schemeName == null || schemeName.isEmpty()) {
//            throw new RuntimeException("表空间不能为空！");
//        }
//        condition.transformStr();
//        QCondition conditionInfo = this.qConditionCurd.getConditionInfo(sourceName, schemeName, tableName);
//        if (conditionInfo == null) {
//            //执行新增
//            condition = this.qConditionCurd.insertConditionInfo(condition);
//        } else {
//            //执行修改
//            condition = this.qConditionCurd.updateConditionInfo(condition);
//        }
//        if (condition != null) {
//            this.qCache.setConditionToCache(condition);
//        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    public int delCondition(String source, String scheme, String tableName) {
//        QCondition qCondition = this.qConditionCurd.deleteConditionInfo(source, scheme, tableName);
//        if (qCondition != null) {
//            this.qCache.clearConditionToCache(source, scheme, tableName);
//        }
        return 1;
    }
}
